Page({
  data:{
    code: -1,
    list: [
      {
        alphabet: 'A', 
        datas: [
          { 
            id: 0,
            title: "阿达帕林凝胶",
            status: 0
          }, 
          { 
            id: 1,
            title: "阿莫西林胶囊",
            status: 0
          }, 
          {
            id: 2,
            title: "艾附暖宫丸",
            status: 0
          }, 
          {
            id: 3,
            title: "安宫牛黄丸",
            status: 0
          }, 
          {
            id: 4,
            title: "奥博林",
            status: 0
          }, 
        ]},
      {
        alphabet: 'B', 
        datas: [
          { 
            id: 5,
            title: "八味檀香丸",
            status: 0
          }, 
          { 
            id: 6,
            title: "八珍颗粒",
            status: 0
          }, 
          {
            id: 7,
            title: "八珍益母胶囊",
            status: 0
          }, 
          {
            id: 8,
            title: "白癜风胶囊",
            status: 0
          }, 
          {
            id: 9,
            title: "板蓝根颗粒",
            status: 0
          }, 
          {
            id: 10,
            title: "苯磺酸氨氯地平片",
            status: 0
          }, 
          {
            id: 11,
            title: "鼻咽灵片",
            status: 0
          }, 
          {
            id: 12,
            title: "鼻炎康片",
            status: 0
          }, 
          {
            id: 13,
            title: "吡拉西坦片",
            status: 0
          }, 
        ]},
      {
        alphabet: 'C', 
        datas: [
          { 
            id: 14,
            title: "参丹活血胶囊",
            status: 0
          }, 
          { 
            id: 15,
            title: "肠多糖片",
            status: 0
          }, 
          {
            id: 16,
            title: "肠炎宁片",
            status: 0
          }, 
          {
            id: 17,
            title: "醋酸地塞米松片",
            status: 0
          }, 
          {
            id: 18,
            title: "醋酸甲地孕酮片",
            status: 0
          }, 
          {
            id: 19,
            title: "醋酸氯己定栓",
            status: 0
          }, 
          {
            id: 20,
            title: "醋酸曲普瑞林注射液",
            status: 0
          }, 
        ]},
      {
        alphabet: 'D', 
        datas: [
          { 
            id: 21,
            title: "达克宁乳膏",
            status: 0
          }, 
          { 
            id: 22,
            title: "胆宁片",
            status: 0
          }, 
          {
            id: 23,
            title: "胆舒软胶囊",
            status: 0
          }, 
          {
            id: 24,
            title: "当归苦参丸",
            status: 0
          }, 
          {
            id: 25,
            title: "地喹氯铵含片",
            status: 0
          }, 
          {
            id: 26,
            title: "多巴丝肼片",
            status: 0
          }, 
        ]},
      {
        alphabet: 'E', 
        datas: [
          { 
            id: 27,
            title: "厄贝沙坦胶囊",
            status: 0
          }, 
          { 
            id: 28,
            title: "厄贝沙坦氢氯噻嗪片",
            status: 0
          }, 
          {
            id: 29,
            title: "二十五味马宝丸",
            status: 0
          }, 
        ]},
      {
        alphabet: 'F', 
        datas: [
          { 
            id: 30,
            title: "泛昔洛韦片",
            status: 0
          }, 
        ]},
      {
        alphabet: 'G', 
        datas: [
          { 
            id: 31,
            title: "谷丙甘氨酸胶囊",
            status: 0
          }, 
        ]},
      {
        alphabet: 'H', 
        datas: [
          { 
            id: 32,
            title: "红霉素眼膏",
            status: 0
          }, 
        ]},
      {alphabet: 'I', 
      datas: [

      ]},
      {alphabet: 'J', 
      datas: [
        { 
          id: 33,
          title: "积雪苷胶囊",
          status: 0
        }, 
        { 
          id: 34,
          title: "甲硝唑乳膏",
          status: 0
        }, 
      ]},
      {alphabet: 'K', 
      datas: [
        { 
          id: 35,
          title: "抗病毒口服液f",
          status: 0
        }, 
      ]},
      {alphabet: 'L', 
      datas: [
        { 
          id: 36,
          title: "来氟米特片",
          status: 0
        }, 
      ]},
      {alphabet: 'M', 
      datas: [
        { 
          id: 37,
          title: "蒙脱石散",
          status: 0
        }, 
      ]},
      {alphabet: 'N', 
      datas: [
        { 
          id: 38,
          title: "诺氟沙星胶囊",
          status: 0
        }, 
      ]},
      {alphabet: 'O', 
      datas: [

      ]},
      {alphabet: 'P', 
      datas: [
        { 
          id: 39,
          title: "脾胃舒丸",
          status: 0
        }, 
        { 
          id: 40,
          title: "葡萄糖酸锌颗粒",
          status: 0
        }, 
      ]},
      {alphabet: 'Q', 
      datas: [
        { 
          id: 41,
          title: "清热消炎宁胶囊",
          status: 0
        }, 
      ]},
      {alphabet: 'R', 
      datas: [
        { 
          id: 42,
          title: "乳酸菌素片",
          status: 0
        }, 
      ]},
      {alphabet: 'S', 
      datas: [
        { 
          id: 43,
          title: "蛇胆川贝散",
          status: 0
        }, 
      ]},
      {alphabet: 'T', 
      datas: [
        { 
          id: 44,
          title: "碳酸氢钠注射液",
          status: 0
        }, 
      ]},
      {alphabet: 'U', 
      datas: [

      ]},
      {alphabet: 'V', 
      datas: [

      ]},
      {alphabet: 'W', 
      datas: [
        { 
          id: 45,
          title: "维A酸胶囊",
          status: 0
        }, 
        { 
          id: 46,
          title: "维生素E软胶囊",
          status: 0
        }, 

      ]},
      {alphabet: 'X', 
      datas: [
        { 
          id: 47,
          title: "夏枯草膏",
          status: 0
        }, 
        { 
          id: 48,
          title: "小儿感冒颗粒",
          status: 0
        }, 
      ]},
      {alphabet: 'Y', 
      datas: [
        { 
          id: 49,
          title: "盐酸米诺环素胶囊",
          status: 0
        }, 
      ]},
      {alphabet: 'Z', 
      datas: [
        { 
          id: 50,
          title: "左氧氟沙星片",
          status: 0
        }, 
      ]},
    ],
    content_array: [],
    alpha: '',
    windowHeight: '',
    input_text: "", //搜索框的值
    history: false, //显示历史记录
    noneview: false, //显示未找到提示
    content_list: false, //显示列表
    historyArray: [], //历史记录数组,
    newArray: [], //添加历史记录数组
  },

  //跳转至详情
  goto_xq: function(e){
    console.log(e.currentTarget.dataset.id)
    var code = e.currentTarget.dataset.id;
    this.setData({
      code: code
    })
    wx.setStorage({
      key: 'code',
      data: code,
    })
    wx.navigateTo({
      url: '../cyxq/cyxq'
    })
  },
  //清除历史记录
  cleanhistory: function(e) {
    this.setData({
      history: false, //隐藏历史记录
      historyArray: [], //清空历史记录数组
      newArray: [],
      input_text: "" //清空搜索框
    })
  },
  //搜索
  search: function(e) {
    var searchtext = this.data.input_text; //搜索框的值
    var sss = true;
    let a='content_array.status';
    this.setData({
      content_list: false, //显示列表
    })
    var content_array=[]
    for(const element of this.data.list){
      for(const element2 of element.datas)
        content_array.push(element2)
    }
    if (searchtext != "") {
      //将搜索框的值赋给历史数组
      this.data.historyArray.push(searchtext);
      //模糊查询 循环查询数组中的title字段
      for (var index in content_array) {
        var num = content_array[index].title.indexOf(searchtext);
        let temp = 'content_array[' + index + '].status';
        if (num != -1) { //不匹配的不显示
          this.setData({
            [temp]: 1,
          })
          sss = false //隐藏未找到提示
        }
        console.log()
      }
      this.setData({
        history: false, //隐藏历史记录
        noneview: sss, //隐藏未找到提示
        content_list: true, //显示列表
        newArray: this.data.historyArray, //给新历史记录数组赋值
        content_array: content_array
      })
    } else {
      this.setData({
        //noneview: true, //显示未找到提示
        content_list: false, //隐藏列表
        history: false, //隐藏历史记录
      })
    }
  },

  //搜索框的值
  search_input: function(e) {
    //当删除input的值为空时
    if (e.detail.value == "") {
      this.setData({
        history: true, //显示历史记录
        content_list: false, //隐藏列表
        noneview:false,
      });
      //所有列表的状态改为0
      for (var index in this.data.content_array) {
        let temp = 'content_array[' + index + '].status';
        this.setData({
          [temp]: 0,
        })
      }
    }
    this.setData({
      input_text: e.detail.value
    })
  },

  //点击历史记录赋值给搜索框
  textfz: function(e) {
    this.setData({
      input_text: e.target.dataset.text
    })
  },

  onLoad(options){
    try {
      var res = wx.getSystemInfoSync()
      this.windowHeight = res.windowHeight
      console.log(this.windowHeight)
      this.apHeight = 16;
      this.offsetTop = 80;
      console.log(this.apHeight);
      console.log(this.offsetTop)
      this.setData({windowHeight: res.windowHeight + 'px'})
    } catch (e) {
      
    }
  },
  handlerAlphaTap(e) {
    console.log(e)
    let {ap} = e.target.dataset;
    this.setData({alpha: ap});
  },
  touchMove(e) {
    console.log(e)
    console.log(this.offsetTop)
    let {list} = this.data;
    console.log(list)
    console.log(this.data.alphabet)
    let moveY = e.touches[0].clientY;
    let rY = moveY - this.offsetTop;
    if(rY >= 0) {
      let index = Math.ceil((rY - this.apHeight)/ this.apHeight);
      if(0 <= index < list.length) {
        let nonwAp = list[index];
        nonwAp && this.setData({alpha: nonwAp.alphabet});
      } 
    }
  }
})

